{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 05\n",
    "\n",
    "# 2 x 2矩阵求逆\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "这段代码的主要目标是计算一个 $2 \\times 2$ 矩阵的逆矩阵。假设我们有矩阵：\n",
    "\n",
    "$$\n",
    "A = \\begin{bmatrix} a & b \\\\ c & d \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "要计算 $A$ 的逆矩阵 $A^{-1}$，首先需要计算矩阵的 **行列式**（determinant）。行列式是一个标量值，它用于判断矩阵是否可逆，计算公式如下：\n",
    "\n",
    "$$\n",
    "\\det(A) = ad - bc\n",
    "$$\n",
    "\n",
    "如果 $\\det(A) = 0$，那么矩阵是**不可逆的**，即不存在 $A^{-1}$。在代码中，这一步通过 `det = a * d - b * c` 计算，并用 `if det == 0:` 判断是否可逆。如果行列式为零，函数会打印 \"矩阵不可逆，行列式为零\"，并返回 `None`，终止计算。\n",
    "\n",
    "如果矩阵是可逆的，即 $\\det(A) \\neq 0$，则可以按照标准公式计算逆矩阵：\n",
    "\n",
    "$$\n",
    "A^{-1} = \\frac{1}{\\det(A)} \\begin{bmatrix} d & -b \\\\ -c & a \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "在代码中，首先创建矩阵：\n",
    "\n",
    "$$\n",
    "\\begin{bmatrix} d & -b \\\\ -c & a \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "然后，将其除以 $\\det(A)$ 来得到最终的逆矩阵。这一步在代码里是 `inv_A = np.array([[d, -b], [-c, a]]) / det`。\n",
    "\n",
    "代码示例中，矩阵 $A$ 被定义为：\n",
    "\n",
    "$$\n",
    "A = \\begin{bmatrix} 1 & 2 \\\\ 3 & 4 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "其行列式计算如下：\n",
    "\n",
    "$$\n",
    "\\det(A) = (1 \\times 4) - (2 \\times 3) = 4 - 6 = -2\n",
    "$$\n",
    "\n",
    "因此，$A$ 是可逆的，按照逆矩阵公式计算：\n",
    "\n",
    "$$\n",
    "A^{-1} = \\frac{1}{-2} \\begin{bmatrix} 4 & -2 \\\\ -3 & 1 \\end{bmatrix}\n",
    "= \\begin{bmatrix} -2 & 1 \\\\ 1.5 & -0.5 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "最终，`inverse_2x2_A(A)` 计算并返回了这个逆矩阵。该方法利用 `numpy` 进行矩阵运算，并确保只对可逆矩阵进行计算，从而避免错误。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7998c9a-044c-4308-b809-c7458b33a48b",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d530bc5e-a5b5-41cf-92e4-461541be8fc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd8fe07d-9741-4d49-bc75-4292daf3790b",
   "metadata": {},
   "source": [
    "## 自定义函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ad0dd3b8-1858-4362-82f0-c17ff891d911",
   "metadata": {},
   "outputs": [],
   "source": [
    "def inverse_2x2_A(A):\n",
    "    \"\"\"\n",
    "    A = [[a, b],\n",
    "         [c, d]]\n",
    "    \"\"\"\n",
    "    # 提取矩阵元素\n",
    "    a, b = A[0]\n",
    "    c, d = A[1]\n",
    "    \n",
    "    # 计算行列式 det(A) = ad - bc\n",
    "    det = a * d - b * c\n",
    "    \n",
    "    # 判断行列式是否为零\n",
    "    if det == 0:\n",
    "        print(\"矩阵不可逆，行列式为零\")\n",
    "        return None\n",
    "    \n",
    "    # 计算逆矩阵\n",
    "    inv_A = np.array([[d, -b], [-c, a]]) / det\n",
    "    return inv_A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5905d558-ffac-4737-868f-7c9b665aeab0",
   "metadata": {},
   "source": [
    "## 定义矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1e029681-047b-42b1-9f57-6476396e95a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2],\n",
       "       [3, 4]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = [[1, 2], \n",
    "     [3, 4]]  # 示例矩阵\n",
    "A = np.array(A)\n",
    "A"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50736aaa-bc64-46c9-9527-8dce6b0ba101",
   "metadata": {},
   "source": [
    "## 计算逆矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "72553dc9-98d5-4509-ab4e-679c3668f3b3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2. ,  1. ],\n",
       "       [ 1.5, -0.5]])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inverse_2x2_A(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d86b9119-2028-4797-8c3e-6c7af8fd2995",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9eac7716-a90e-4c76-ac0f-c81c864dd073",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3886cb7b-972c-4347-9b6b-f66c8de25cbb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "070c3389-8048-43a3-baa7-6666009bce96",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
